// pages/mangox/gettxs/gettxs.js
var base64 = require('../../../dist/example/images/base64');

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userHash: null,//123***123格式化
    toUserHash: null,
    amount: 0,
    toAmount: "", //赠送额
    userName: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //未登陆就跳转到登陆授权页面
    if (!app.globalData.userInfo){
      wx.redirectTo({
        url: '/pages/user/auth/auth'
      })
      return;
    }

    var h = app.globalData.userHash;
    h = h.substr(0, 15) + "***";
    this.setData({
      userHash: h.substring(0, 5) + "***" + h.substring(h.length - 5),
      amount: app.globalData.loginInfo.amount,
      userName: app.globalData.loginInfo.Data.UserName,
    });

  },
  //送金币
  transfer: function () {
    var that = this;
    
    if (!app.globalData.userHash) {
      wx.showModal({
        content: '请先登陆',
        showCancel: false
      })
      return false;
    }
    if (!that.data.toUserHash) {
      wx.showModal({
        content: '请填写收金币人',
        showCancel: false
      })
      return false;
    }
    if (!that.data.toAmount) {
      wx.showModal({
        content: '请输入金币',
        showCancel: false
      })
      return false;
    }

    wx.request({
      url: app.globalData.urlPath + '/api/Mangox/Transfer',
      method: 'POST',
      data: {
        Token: app.globalData.loginInfo.Token,
        OpenID: app.globalData.openid,
        Source: app.globalData.appSource,
        Param: {
          userHash: app.globalData.userHash,
          //amount: app.globalData.loginInfo.amount,
          toUserHash: that.data.toUserHash,
          toAmount: that.data.toAmount,
          amount: that.data.amount,//用于计算余数
        },
      },
      success: function (res) {
        if(res.data.Code == 0){
          console.log("赠送成功：" + res);
          // wx.showModal({
          //   content: '赠送成功',
          //   showCancel: false
          // });
          that.setData({
            amount: res.data.Data,
          })
          app.globalData.loginInfo.amount = res.data.Data;
          wx.showModal({
            content: '赠送成功',
            showCancel: false,
            success: function (res) {
                if (res.confirm) {                  
                    console.log('用户点击确定')
                    wx.switchTab({
                      url: '/pages/index/index', 
                      success: function (e) { 
                        //跳转后刷新                        
                        var page = getCurrentPages().pop(); 
                        if (page == undefined || page == null) return; 
                          page.onLoad(); 
                      } 
                    })
                    return
                }
            }
          });
        }else{
          console.log("赠送失败：" + JSON.stringify(res.data));
          wx.showModal({
            content: '赠送失败:' + res.data.Msg,
            showCancel: false
          });
        }
      }
    });
  },
  bindBlurToHash: function (e) {
    this.setData({
      toUserHash: e.detail.value,
    });
  },
  bindBlurToAmount: function (e) {
    this.setData({
      toAmount: e.detail.value,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})